var svgDoc = null;

var boxy = null;

$(function(){
	$('.boxy').boxy();
});

function getSvgDoc()
{
	if(svgDoc == null)
	{
		svgDoc = document.getElementById('emb1').getSVGDocument();
	}
	return svgDoc;
}

function startDblClick(startNode)
{
	var startId = $('#startId');
	var status = $('#startStatus');
	var startName = $('#startName');
	var actors = $('#startActors');
	var actorsDiv = $('#startActorsDiv');
	startId.val(startNode.id);
	status.val(startNode.status);
	startName.val(startNode.name);
	actors.val(startNode.actors);
	if(isNotNull(startNode.actors))
	{
		actorsDiv.text('已绑定');
	}
	else
	{
		actorsDiv.text('未绑定');
	}
	boxy = new Boxy($('#startDiv'), {title:'开始', modal:true});
}

function saveStart()
{
	var startId = $('#startId');
	var status = $('#startStatus');
	var startName = $('#startName');
	var actors = $('#startActors');
	if(!isNotNull(startName.val()))
	{
		showMsg('名称不能为空。');
		return;
	}
	var startNode = {};
	startNode.id = startId.val();
	startNode.status = status.val();
	startNode.name = startName.val();
	startNode.actors = actors.val();
	getSvgDoc().window.saveStart(startNode);
	boxy.hide();
	boxy = null;
}

function toSelectActors(nodeType)
{
	var actors = $('#' + nodeType + 'Actors');
	var url = realPath + '/inner/flow/actorsSelect.action?actors=' + actors.val() + '&nodeType=' + nodeType;
	var val = showMd(url, 500, 600, 'yes', 'yes');
	if(val != undefined)
	{
		actors.val(val);
	}
}

function taskActorsModeChange()
{
	var taskActorsMode = $('#taskActorsMode');
	var taskActors = $('#taskActors');
	var taskActorsModeCode = $('#taskActorsModeCode');
	var taskActorsModeCodeTb = $('#taskActorsModeCodeTb');
	var taskActorsTb = $('#taskActorsTb');
	var taskActorsDiv = $('#taskActorsDiv');
	if('select' == taskActorsMode.val())
	{
		taskActorsModeCode.val('');
		taskActorsModeCodeTb.css('display', 'none');
		taskActorsTb.css('display', 'inline-block');
	}
	else if('code' == taskActorsMode.val())
	{
		taskActors.val('');
		taskActorsDiv.text('未绑定');
		taskActorsModeCodeTb.css('display', 'inline-block');
		taskActorsTb.css('display', 'none');
	}
}

function taskConditionTypeChange()
{
	var taskConditionType = $('#taskConditionType');
	var taskPercentage = $('#taskPercentage');
	if('percentage' == taskConditionType.val())
	{
		taskPercentage.css('display', 'inline-block');
	}
	else
	{
		taskPercentage.css('display', 'none');
	}
}

function taskDblClick(taskNode)
{
	var taskId = $('#taskId');
	var status = $('#taskStatus');
	var taskName = $('#taskName');
	var actors = $('#taskActors');
	var taskActorsMode = $('#taskActorsMode');
	var taskActorsModeCode = $('#taskActorsModeCode');
	var taskAssignToUser = $('#taskAssignToUser');
	var taskConditionType = $('#taskConditionType');
	var taskPercentage = $('#taskPercentage');
	var taskPermitReturn = $('#taskPermitReturn');
	var taskModifyCode = $('#taskModifyCode');
	var actorsDiv = $('#taskActorsDiv');
	taskId.val(taskNode.id);
	status.val(taskNode.status);
	taskName.val(taskNode.name);
	actors.val(taskNode.actors);
	taskActorsMode.val(taskNode.actorsMode);
	taskActorsModeCode.val(taskNode.auditorCode);
	taskAssignToUser.val(taskNode.assignToUser);
	taskConditionType.val(taskNode.conditionType);
	taskPercentage.val(taskNode.percentage);
	taskPermitReturn.val(taskNode.permitReturn);
	taskModifyCode.val(taskNode.modifyCode);
	
	if(isNotNull(taskNode.actors))
	{
		actorsDiv.text('已绑定');
	}
	else
	{
		actorsDiv.text('未绑定');
	}
	taskActorsModeChange();
	taskConditionTypeChange();
	boxy = new Boxy($('#taskDiv'), {title:'任务', modal:true});
}

function saveTask()
{
	var taskId = $('#taskId');
	var status = $('#taskStatus');
	var taskName = $('#taskName');
	var actors = $('#taskActors');
	var taskActorsMode = $('#taskActorsMode');
	var taskActorsModeCode = $('#taskActorsModeCode');
	var taskAssignToUser = $('#taskAssignToUser');
	var taskConditionType = $('#taskConditionType');
	var taskPercentage = $('#taskPercentage');
	var taskPermitReturn = $('#taskPermitReturn');
	var taskModifyCode = $('#taskModifyCode');
	if(!isNotNull(taskName.val()))
	{
		showMsg('名称不能为空。');
		return;
	}
	var taskNode = {};
	taskNode.id = taskId.val();
	taskNode.status = status.val();
	taskNode.name = taskName.val();
	taskNode.actors = actors.val();
	taskNode.actorsMode = taskActorsMode.val();
	taskNode.auditorCode = taskActorsModeCode.val();
	taskNode.assignToUser = taskAssignToUser.val();
	taskNode.conditionType = taskConditionType.val();
	taskNode.percentage = taskPercentage.val();
	taskNode.permitReturn = taskPermitReturn.val();
	taskNode.modifyCode = taskModifyCode.val();
	getSvgDoc().window.saveTask(taskNode);
	boxy.hide();
	boxy = null;
}

function endDblClick(endNode)
{
	var endId = $('#endId');
	var status = $('#endStatus');
	var endName = $('#endName');
	endId.val(endNode.id);
	status.val(endNode.status);
	endName.val(endNode.name);
	boxy = new Boxy($('#endDiv'), {title:'结束', modal:true});
}

function saveEnd()
{
	var endId = $('#endId');
	var status = $('#endStatus');
	var endName = $('#endName');
	if(!isNotNull(endName.val()))
	{
		showMsg('名称不能为空。');
		return;
	}
	var endNode = {};
	endNode.id = endId.val();
	endNode.status = status.val();
	endNode.name = endName.val();
	getSvgDoc().window.saveEnd(endNode);
	boxy.hide();
	boxy = null;
}

function textDblClick(textNode)
{
	var textId = $('#textId');
	var textName = $('#textName');
	textId.val(textNode.id);
	textName.val(textNode.name);
	boxy = new Boxy($('#textDiv'), {title:'文本', modal:true});
}

function saveText()
{
	var textId = $('#textId');
	var textName = $('#textName');
	if(!isNotNull(textName.val()))
	{
		showMsg('名称不能为空。');
		return;
	}
	var textNode = {};
	textNode.id = textId.val();
	textNode.name = textName.val();
	getSvgDoc().window.saveText(textNode);
	boxy.hide();
	boxy = null;
}

function arrowDblClick(lineArrow)
{
	var arrowId = $('#arrowId');
	var arrowConditionCode = $('#arrowConditionCode');
	var arrowActionCode = $('#arrowActionCode');
	var arrowSort = $('#arrowSort');
	arrowId.val(lineArrow.id);
	arrowConditionCode.val(lineArrow.conditionCode);
	arrowActionCode.val(lineArrow.actionCode);
	arrowSort.val(lineArrow.sort);
	boxy = new Boxy($('#arrowDiv'), {title:'流转', modal:true});
}

function saveArrow()
{
	var arrowId = $('#arrowId');
	var arrowConditionCode = $('#arrowConditionCode');
	var arrowActionCode = $('#arrowActionCode');
	var arrowSort = $('#arrowSort');
	if(checkInteger(arrowSort) == false)
	{
		showMsg('排序必须为整数。', function(){arrowSort.focus();});
		return;
	}
	var lineArrow = {};
	lineArrow.id = arrowId.val();
	lineArrow.conditionCode = arrowConditionCode.val();
	lineArrow.actionCode = arrowActionCode.val();
	lineArrow.sort = arrowSort.val();
	getSvgDoc().window.saveArrow(lineArrow);
	boxy.hide();
	boxy = null;
}

function saveFlow(xml, idSeq)
{
	var flowType = $('#flowType');
	var content = $('#content');
	var flowName = $('#flowName');
	var maxId = $('#maxId');
	maxId.val(idSeq);
	var url = '';
	if('add' == flowType.val())
	{
		url = 'inner/flow/addFlow.action';
	}
	else if('update' == flowType.val())
	{
		url = 'inner/flow/updateFlow.action';
	}
	if(!isNotNull(flowName.val()))
	{
		showMsg('流程名称不能为空。', function(){
			flowName.focus();
		});
		return;
	}
	content.val(xml);
	ajaxAction(url, 'post', 'frm', false, function(){
		window.opener.searchThis();
		window.close();
	});
}



